package com.data.modules.health.service.impl;

import com.data.modules.health.entity.HlUser;
import com.data.modules.health.entity.HlContactsInfo;
import com.data.modules.health.entity.HlUserBed;
import com.data.modules.health.entity.HlUserHealth;
import com.data.modules.health.mapper.HlContactsInfoMapper;
import com.data.modules.health.mapper.HlUserBedMapper;
import com.data.modules.health.mapper.HlUserHealthMapper;
import com.data.modules.health.mapper.HlUserMapper;
import com.data.modules.health.service.IHlUserService;
import com.data.modules.health.vo.BdeDeviceReqVo;
import com.data.modules.health.vo.DedResVo;
import com.data.modules.smzh.vo.DeviceReqVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 老人档案信息
 * @Author: data-boot
 * @Date:   2023-07-17
 * @Version: V1.0
 */
@Service
public class HlUserServiceImpl extends ServiceImpl<HlUserMapper, HlUser> implements IHlUserService {


	@Autowired
	private HlUserMapper hlUserMapper;
	@Autowired
	private HlContactsInfoMapper hlContactsInfoMapper;
	@Autowired
	private HlUserHealthMapper hlUserHealthMapper;
	@Autowired
	private HlUserBedMapper hlUserBedMapper;

	@Override
	@Transactional
	public void saveMain(HlUser hlUser, List<HlContactsInfo> hlContactsInfoList,List<HlUserHealth> hlUserHealthList,List<HlUserBed> hlUserBedList) {
		hlUserMapper.insert(hlUser);
		if(hlContactsInfoList!=null && hlContactsInfoList.size()>0) {
			for(HlContactsInfo entity:hlContactsInfoList) {
				//外键设置
				entity.setUserId(hlUser.getId());
				hlContactsInfoMapper.insert(entity);
			}
		}
		if(hlUserHealthList!=null && hlUserHealthList.size()>0) {
			for(HlUserHealth entity:hlUserHealthList) {
				//外键设置
				entity.setUserId(hlUser.getId());
				hlUserHealthMapper.insert(entity);
			}
		}
		if(hlUserBedList!=null && hlUserBedList.size()>0) {
			for(HlUserBed entity:hlUserBedList) {
				//外键设置
				entity.setUserId(hlUser.getId());
				hlUserBedMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(HlUser hlUser,List<HlContactsInfo> hlContactsInfoList,List<HlUserHealth> hlUserHealthList,List<HlUserBed> hlUserBedList) {
		hlUserMapper.updateById(hlUser);

		//1.先删除子表数据
		hlContactsInfoMapper.deleteByMainId(hlUser.getId());
		hlUserHealthMapper.deleteByMainId(hlUser.getId());
		hlUserBedMapper.deleteByMainId(hlUser.getId());

		//2.子表数据重新插入
		if(hlContactsInfoList!=null && hlContactsInfoList.size()>0) {
			for(HlContactsInfo entity:hlContactsInfoList) {
				//外键设置
				entity.setUserId(hlUser.getId());
				hlContactsInfoMapper.insert(entity);
			}
		}
		if(hlUserHealthList!=null && hlUserHealthList.size()>0) {
			for(HlUserHealth entity:hlUserHealthList) {
				//外键设置
				entity.setUserId(hlUser.getId());
				hlUserHealthMapper.insert(entity);
			}
		}
		if(hlUserBedList!=null && hlUserBedList.size()>0) {
			for(HlUserBed entity:hlUserBedList) {
				//外键设置
				entity.setUserId(hlUser.getId());
				hlUserBedMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		hlContactsInfoMapper.deleteByMainId(id);
		hlUserHealthMapper.deleteByMainId(id);
		hlUserBedMapper.deleteByMainId(id);
		hlUserMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			hlContactsInfoMapper.deleteByMainId(id.toString());
			hlUserHealthMapper.deleteByMainId(id.toString());
			hlUserBedMapper.deleteByMainId(id.toString());
			hlUserMapper.deleteById(id);
		}
	}

	@Override
	public DedResVo queryBedDeviceSynInfo(BdeDeviceReqVo reqVo) {

		return hlUserMapper.searchBedDeviceSynInfo(reqVo);
	}

}
